<?php
/******************************************************* *   Copyright (C) 2007  http://p3net.net    This program is free software; you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation; either version 2 of the License, or    (at your option) any later version.    This program is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.    You should have received a copy of the GNU General Public License along    with this program; if not, write to the Free Software Foundation, Inc.,    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    
    @id: $Id: images.php 5 2007-07-26 23:43:48Z p3net.tech $
*********************************************************/
include('globals.php');
class image
{
	function full($id)
	{
		$id = is_numeric($_GET["id"]) ? $_GET["id"] : null;
		if(empty($id))
		{
			$error->general("Invalid ID specified", "Not an (int)");
		}
		$_query = "SELECT * FROM `images` WHERE `id`='" . $id . "'";
		$img=$db->fetch_array($db->query($_query));
		
		header('Content-type: ' . $img['mine_type']);
		header('Content-Disposition: attachment; filename=' . $img['name']);
		echo $img['content'];
	}
	function thumb($id)
	{
		$id = is_numeric($_GET["id"]) ? $_GET["id"] : null;
		if(empty($id))
		{
			$error->general("Invalid ID specified", "Not an (int)");
		}
		$_query = "SELECT * FROM `images` WHERE `id`='" . $id . "'";
		$img=$db->fetch_array($db->query($_query));
		
		/* We're going to resize the larger dimension to 150px */
		if($img['width'] > $img['height'])
		{
			$scale_percentage = $img['width'] / 150;
		}
		else
		{
			$scale_percentage = $img['height'] / 150;
		}
		$new_dimensions = array(
			'width' => ($scale_percentage < 1) ? $img['width'] * $scale_percentage : $img['width'],
			'height' => ($scale_percentage < 1) ? $img['height'] * $scale_percentage : $img['height']
		);
		header('Content-type: ' . $img['mine_type']);
		header('Content-Disposition: attachment; filename=' . $img['name']);
		
		$type = explode("/", $img['mine_type']);
		$type = $type[1];
		switch $type
		{
			case 'jpeg':
				$new_image = imagecreatefromjpeg($img['content']);
				break;
			case 'png':
				$new_image = imagecreatefrompng($img['content']);
				break;
			case 'gif':
				$new_image = imagecreatefromgif($img['content']);
				break;
		}
		$res = imagecreatetruecolor($new_dimensions['width'], $new_dimensions['height']);
		imagecopyresized($res, $new_image, 0, 0, 0, 0, $new_dimensions['width'], $new_dimensions['height'], $img['width'], $img['height']);
		
		switch $type
		{
			case 'jpeg':
				imagejpeg($res);
				break;
			case 'png':
				imagepng($res);
				break;
			case 'gif':
				imagegif($res);
				break;
		}
	}
}

$this =& new image;

/* Actually handle the data here */
$mode=$_GET["mode"];
switch $mode
{
	case 'view':
		$this->full();
		break;
		
	case 'thumb':
		$this->thumb();
		break;
}
?>